package wx.controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import wx.core.Control;
	import wx.core.ITheme;
	import wx.core.SettingCache;
	
	
	
	/**
	 * 
	 * @author wujianqi
	 * 
	 * 滑块控件
	 * 
	 */	
	
	[Event(name="change",type="flash.events.Event")]	

	public class Slider extends Control
	{
		
		private var _trackSkin:DisplayObject;
		private var _sliddenSkin:DisplayObject;
		private var _button:ButtonBase;
		
		private var _maximum:Number = 100; //最大值
		private var _minimum:Number = 0; //最小值
		private var _step:Number = 0; //步长
		
		private var _width:Number;
		private var _value:Number = _minimum;	
		private const _theme:ITheme = SettingCache.theme;
		
		public function Slider()
		{
			super();
			_trackSkin = _theme.slideTrackBgSkin;
			_sliddenSkin = _theme.slideTrackOnSkin;			
			validSkinItem(_trackSkin);
			validSkinItem(_sliddenSkin);
			
			
			var up:DisplayObject = _theme.sliderButtonSkin;
			var hover:DisplayObject = _theme.sliderButtonHoverSkin;
			var down:DisplayObject =  _theme.sliderButtonDownSkin
			var disabled:DisplayObject =  _theme.sliderButtonSkin;	
			_button = new ButtonBase(up,hover,down,disabled);
			
			_width = _trackSkin.width - _button.width;
			addTrack();
			
			if(_enable) bindEvent();
		}
		
		public function get value():Number
		{
			return _value;
		}
		public function set value(num:Number):void
		{
			if(_value != num){
				_value = num;
				var currentX:Number = num *_width/(_maximum - _minimum);
				_button.x = currentX;
				if(num>_minimum){
					_sliddenSkin.visible = true;
					_sliddenSkin.width = currentX + _button.width/2;
				}else
					_sliddenSkin.visible = false;
			}
		}

		private function onStart(e:MouseEvent):void{
			var rect:Rectangle = new Rectangle(0, 0, _width, 0);
			_button.startDrag(false,rect);			
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,onEnd);
		}
		private function onEnd(e:MouseEvent):void{
			_button.stopDrag();
			dispatchEvent(new Event(Event.CHANGE));
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP,onEnd);
		}
		private function onMove(e:MouseEvent):void{
			if(e.buttonDown){
				if(_button.x>0){
					_sliddenSkin.visible = true;
					var innerWidth:Number = _button.width/2,
						currentX:Number =  _button.x+ innerWidth;
					_sliddenSkin.width = currentX;
					_value = Math.ceil((_maximum - _minimum)*(currentX-innerWidth)/_width);
				}else{
					_sliddenSkin.visible = false;
					_value = _minimum;
				}
				e.updateAfterEvent();
			}
		}
		
		private function bindEvent():void{
			_button.addEventListener(MouseEvent.MOUSE_DOWN,onStart);
		}		
		private function unbindEvent():void{			
			_button.removeEventListener(MouseEvent.MOUSE_DOWN,onStart);
		}
		
		
		private function addTrack():void{
			var hmax:Number = Math.max(_trackSkin.height,_button.height);
			var hmin:Number = Math.min(_trackSkin.height,_button.height);			
			_trackSkin.y = (hmax-hmin)/2;
			_sliddenSkin.y = (hmax-hmin)/2;
			_sliddenSkin.visible = false;
			addChild(_trackSkin);
			addChild(_sliddenSkin);
			addChild(_button);
		}

		override protected function dispose():void{
			unbindEvent();
		}
		
	}
}